﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;

namespace DataLayer.Model
{
    public class Lancamento
    {
        public Guid ID { get; set; }
        public decimal Valor { get; set; }
        public string Descricao { get; set; }
        public DateTime DataVencimento { get; set; }
        public DateTime DataPagamento { get; set; }
        public Categoria Categoria { get; set; }
        public Conta Conta { get; set; }

        public void Salvar()
        {
            this.ID = Guid.NewGuid();
            IObjectContainer client = DataLayer.Core.Server.OpenClient();

            this.Categoria = (from Categoria p in client
                              where p.ID == this.Categoria.ID
                              select p).Single();

            this.Conta = (from Conta p in client
                              where p.ID == this.Conta.ID
                              select p).Single();

            client.Store(this);

            try
            {
                client.Commit();
            }
            catch (Exception)
            {
                client.Rollback();
            }



            DataLayer.Core.Server.CloseClient(client);
        }

        public static List<Lancamento> GetAll()
        {
            IObjectContainer client = DataLayer.Core.Server.OpenClient();

            List<Lancamento> lancs = (from Lancamento p in client
                                      select p).ToList();
            DataLayer.Core.Server.CloseClient(client);

            return lancs;
        }

        public static object GetAllEntradas()
        {
            IObjectContainer client = DataLayer.Core.Server.OpenClient();

            List<Lancamento> lancs = (from Lancamento p in client
                                      where p.Valor > 0
                                      select p).ToList();
            DataLayer.Core.Server.CloseClient(client);

            return lancs;
        }

        public static object GetAllSaidas()
        {
            IObjectContainer client = DataLayer.Core.Server.OpenClient();

            List<Lancamento> lancs = (from Lancamento p in client
                                      where p.Valor < 0
                                      select p).ToList();
            DataLayer.Core.Server.CloseClient(client);

            return lancs;
        }
    }
}
